<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller
{
    protected $Response;

    /**
     * Index Page for this controller.
     */
    public function __construct()
    {
        parent::__construct();
        $this->Response = array('code' => 201, 'message' => '', 'result' => array());
    }

    public function index()
    {
        if (gCookie('lg_user_sign')) {
            redirect('/home/index', 'location', 301);
        }
        $this->load->view('welcome/index');
    }

    public function logOut()
    {
        sCookie('lg_user_sign', '');
        redirect('/welcome/index');
    }

    public function runLogin()
    {
        $this->load->library('userlogin');
        if ($this->input->is_ajax_request()) {

            //获取参数，并赋值给接口参数
            $sendData = array();
            $sendData['account'] = $this->input->get_post('userName');
            $sendData['platform'] = 'IOS';
            $sendData['uuid'] = 'WeiXin_001';
            $sendData['password'] = $this->input->get_post('userPwd');
            $this->userlogin->set_call_items($sendData);

            //制造接口请求
            $url = $this->userlogin->set_resource_id(1);
            $param = $this->userlogin->get_call_items();
            $rs = $this->restcurl->get($url, $param);

            if ($rs['ErrorCode'] == 0 && $rs['Success'] == 1) {
                $rs['CallInfo']['password'] = md5($sendData['password']);
                sCookie('lg_user_sign', LKEncode(json_encode($rs['CallInfo']), HASHKEY));
                $this->Response['code'] = 200;
                $this->Response['message'] = '登录成功';
            } else {
                $this->Response['message'] = $rs['ErrorMsg'];
            }
        } else {
            $this->Response['message'] = '非法请求';
        }

        echo json_encode($this->Response);
        die();
    }
}
